const alltxt = require('alltxt');
const global = require('global');
//import guanka from './guanka';
cc.Class({
    extends: cc.Component,

    properties: {
		mubunode:cc.Node,
		rolenode:cc.Node,
		lognode:cc.Node,
		headnode:cc.Node,
		panelnode:cc.Node,
		itemnode:cc.Node,
		titlenode:cc.Node,
		title:cc.Label,
		logtxt:cc.Label,
		menu:cc.Prefab,
		choicepanel:cc.Prefab,
		itempanel:cc.Prefab,
		overclear:cc.Prefab,
		bgsprite:cc.Sprite,
		bgsprite2:cc.Sprite,
		headicon:cc.Sprite,
		heimu:cc.Sprite,
		headAtlas:cc.SpriteAtlas,
		damageZDH:cc.Node,
		zhiliaoZDH:cc.Node,
		leftbtn:cc.Node,
		rightbtn:cc.Node,
		//damageZDH:
		bgarr:[cc.Sprite],
		_gkid:0,//关卡id
		_logid:0,//对话id
		_pasue:0,//=1则为暂时关闭触摸监听，=0恢复触摸监听
    },

    onLoad () {
		this.mubunode.active = true;
		cc.director.once('gamescene',(e)=>{
			cc.log('gs:e.detail='+e.detail+',of='+typeof(e.detail));
			/* this._gkid = e.detail;
			//通过gameinit()来初始化角色图片的位置比较好
			this.gameinit(e.detail);

			this._logid = global.titleinfo[e.detail].startlogid;
			this.title.string = global.titleinfo[e.detail].name;
			this.titlemovie(); */
			this.startstory(e.detail);
		},this);
		//监听选择框按钮点击操作，用来跳转显示下一句话等操作
   		cc.director.on('choicebtn',(e)=>{
				this._logid = e.detail;
				let count = this.panelnode.childrenCount;
				if(count > 0){
					this.panelnode.removeAllChildren()
					cc.log('gs:on choicebtn==count > 0');
				}
				//这里可能要加个检测判断条件的，暂时用true代替
				this.lognode.opacity = 255;
				global._pasue = 0;
				
				//此处加判断e.detail == 89 本来是
				if(e.detail == 89){
					cc.log('gs:on choicebtn==e.detail=89--跳转主界面----');
					//此处的90应该用this.logid的，但是当下代码中this.logid 在触发操作时 其数值都是比设计数值小1
					//所以此处的90本来是即将跳转第三章的内容的，改成跳转主场景
					this.saveend(90);
					
					//注释下面一行代码，在游戏中点击到这个地步时只需要再点击一次 就会触发 游戏结束 界面
					cc.director.loadScene('main');
				}else{
					this.showlogtxt(this._logid);
				}
				
				cc.log('gs:on choicebtn==e.detail='+e.detail);
				
		},this);
		//cc.log('alltxt='+JSON.stringify(alltxt)+'\n of='+typeof(alltxt));
		
    },

    start () {
		//监听各种按钮的操作
		cc.director.on('ongame',(e)=>{
			cc.log('gs:ongame==e.detail='+e.detail);
			this.panelnode.removeAllChildren();
			if (e.detail == 'restart'){//重新开始
				this.mubunode.active = true;
				this.mubunode.opacity = 255;
				//重新开始 的理解为从本场景重新开始章节
				this.startstory(this._gkid);
				/* cc.director.getScheduler().schedule(() => {
					this.mubunode.opacity -= 25;
					if(this.mubunode.opacity <= 25){
						this.mubunode.active = false;
					}
				}, this, 1/10, 10); */
				
				cc.log('gs:ongame=---------=restart');
			}else if(e.detail == 'endcurrent'){//结束本关
				//结束本关 的理解为 回到主场景开始界面
				//此处应该用渐隐效果
				cc.director.getScheduler().schedule(() => {
					this.node.opacity -= 25;
					if(this.node.opacity <= 25){
						this.node.x = 5280;
					}
				}, this, 1/10, 9);
				cc.director.emit('changebg',{name:'music'});
			}else if(e.detail == 'looktips'){//显示提示looktips
				let sate = this.lognode.active,self = this;
				this.lognode.active = true;
				this.headnode.active = false;
				this.logtxt.node.x = -615;
				//此处的提示文本仅作展示，正式开发应该有专门的全局变量来做提示文本的
				this.logtxt.string ='显示提示文字';
				setTimeout(function(){
					//this.logtxt.string ='';
					self.lognode.active = sate;
					self.showlogtxt(self._logid);
				},2000);
			}else if(e.detail == 'next'){//进入下一关
				this._gkid += 1;
				this.startstory(this._gkid);
				//能点下一关的地方已经超出序章内容了，所以直接判断关卡进度
				if(global.gkjindu <= this._gkid){
					global.gkjindu = this._gkid;
					cc.log('saveend:this._gkid='+this._gkid);
				}
			}
		},this);
		//监听获得道具
		cc.director.on('getitem',(e)=>{
			if (e.detail == 'xiangshui'){//重新开始
				let xsitem = this.itemnode.getChildByName('xiangshuiSprite').getComponent(cc.Sprite);
				xsitem.node.runAction(cc.moveTo(1,cc.p(552, 138)));
			}else if(e.detail == 'xiunv'){//结束本关
				let xnitem = this.itemnode.getChildByName('xiunvSprite').getComponent(cc.Sprite);
				xnitem.node.runAction(cc.moveTo(1,cc.p(552, -1)));
			}
		},this);
	},

	//标题动画
	titlemovie(){
		//初始：x=200,opacity=0 渐现（x=0） 渐隐（x=-200）
		//this.title.node.x = 200;
		//两个动作
		let act1 = cc.spawn(cc.moveTo(1,cc.p(0,0)),cc.fadeIn(1));
		let act2 = cc.spawn(cc.moveTo(1,cc.p(-200,0)),cc.fadeOut(1));
		//先后执行动作
		let fadeinout = cc.sequence(act1,act2);
		this.title.node.runAction(fadeinout);
		//结束黑幕并开始场景
		let self = this;
		setTimeout(function(){
			self.closeheimu();
			let logid = global.titleinfo[self._gkid].startlogid;
			if(logid > 0){//章节有起始对话id
				self.showlogtxt(self._logid);
			}else{//章节没有起始对话id则隐藏对话框
				self.lognode.opacity = 0;
			}
			
		},2000);
		
	},

	//伤害动画，传入节点：在哪个角色上面放受伤的帧动画
	damagemovie(targetnode){
		let djs=1;
		let pos = targetnode.getPosition();
		this.damageZDH.position = pos;
		cc.log('damagemovie:pos='+pos.x+'y='+pos.y);
		cc.director.getScheduler().schedule(() => {
			--djs;
			//console.log('djs=' + djs);
			if (djs <= 0){				
				this.damageZDH.opacity = 0;
				this.damageZDH.x = 2000;
				this.damageZDH.y = 0;
			}
		}, this, 1, -1);
	},
		
	//显示log操作
	showlogtxt(logidx){
		global._pasue = 1;
		if(this.logtxt.string.length >0){
			this.logtxt.string ='';
		}
		let role = parseInt(alltxt[logidx].role);
		//cc.log('gs156:role id='+role);
		if(role <= 0){
			this.headnode.active = false;
			this.logtxt.node.x = -615;
		}else{
			this.headnode.active = true;
			let headkey = 'head'+role;
			this.headicon.spriteFrame = this.headAtlas.getSpriteFrame(headkey);
			this.logtxt.node.x = -472;
		}
		
 	/* 	let j = 0,des = alltxt[this._logid].des;
		this.schedule(function() {
			this.logtxt.string += des[j];
			j++;
		},0.1, des.length - 1, 0.1);	 */
		this.logtxt.string = alltxt[logidx].des;
		cc.log('showlogtxt==logidx:'+logidx);
		//如果有动作ACT时则显示动作
		let animationid = alltxt[logidx].animation;
		if(animationid > 0){
			let self = this;
			setTimeout(function(){
				cc.log('====showlogtxt==setTimeout了');
				self.actfunc(animationid);	
			},1000);
			
		}
		global._pasue = 0;
	},
	//显示道具操作
	useitem(event,customData){
		//道具格子坐标x：552 y上：277 138 -1 -140 -279
		let jiangshi = this.rolenode.getChildByName('role7').getComponent(cc.Sprite);
		let nvqishi = this.rolenode.getChildByName('role9').getComponent(cc.Sprite);
		if(customData == 'xiunvitem'){
			//此处是为了保证玩家是在有僵尸的页面使用 修女 的
			if(this.leftbtn.active == true){
				this.zhiliaoZDH.x = jiangshi.node.x;
				this.zhiliaoZDH.y = jiangshi.node.y - 150;
				cc.log(',,this.zhiliaoZDH.x='+this.zhiliaoZDH.x);
				//翻转僵尸图片，延迟显示女骑士并取消治疗光效
				let self = this;
				setTimeout(function(){
					jiangshi.node.runAction(cc.scaleTo(0.5,0,1));
				},1000);
				setTimeout(function(){
					nvqishi.node.runAction(cc.scaleTo(0.5,1,1));
					self.zhiliaoZDH.x = 5000;
					self.zhiliaoZDH.y = 0;
				},1500);
				setTimeout(function(){
					self.lognode.opacity = 255;
					
				},1800);
				this._logid = 93;
				this.showlogtxt(this._logid);
			}
			
			let xnitem = this.itemnode.getChildByName('xiunvSprite').getComponent(cc.Sprite);
			xnitem.node.runAction(cc.moveTo(1,cc.p(1000, -1)));
		}else if(customData == 'xiangshuiitem'){
			cc.log('useitem=====xiangshuiitem');
			global._pasue = 0;
			this.lognode.opacity = 255;
			this._logid = 87;
			this.showlogtxt(this._logid);
			let xsitem = this.itemnode.getChildByName('xiangshuiSprite').getComponent(cc.Sprite);
			xsitem.node.runAction(cc.moveTo(1,cc.p(1000, 138)));
		}
	},
	//结束黑幕
	closeheimu(){
		this.title.node.x = 200;
		cc.director.getScheduler().schedule(() => {
			this.mubunode.opacity -= 10;
			if(this.mubunode.opacity < 10){
				this.mubunode.active = false;
			}
		}, this, 1/25, 24);
		
	},
	//点击触摸事件响应函数===废弃，写法有误
/* 	aaclickTouch(){
		if(global._pasue <=0 ){//暂停触摸相应事件
			let jumpid = parseInt(alltxt[this._logid].jump);
			cc.log('========jumpid='+jumpid);
			if(jumpid > 0){//有跳转对话id则跳转到对应的id
				this._logid += 1;
				this.showlogtxt(jumpid);
				
			}else if(jumpid == 0){//没有后面的话了
				cc.log('========没有跳转对话id了');

				//this.showlogtxt(this._logid);
				let self = this;
				setTimeout(function(){
					self.lognode.opacity = 0;
				},1000);
				
				
			}else{//jumpid = -1 即有end结局的情况下 没有跳转对话id则为下一句
				cc.log('========有end结局了');
				global._pasue = 1;
				this.showlogtxt(this._logid);
				
			}

			//这里可能要加个检测判断条件的，暂时用true代替
			
			cc.log('clickTouch:this._logid='+this._logid);	
		}
	}, */
	//点击触摸事件响应函数
	clickbtn(){
		if(global._pasue <=0 ){//暂停触摸相应事件	
			//this._logid += 1;
			
			let jumpid = parseInt(alltxt[this._logid].jump);
			cc.log('========jumpid='+jumpid+',this._logid='+this._logid);
			if(jumpid > this._logid){//有跳转对话id则跳转到对应的id
				this._logid = jumpid;
				this.showlogtxt(jumpid);
				
			}else if(jumpid == this._logid){//没有后面的话了
				
				global._pasue = 1;
				this.showlogtxt(this._logid);
				let self = this;
				setTimeout(function(){
					cc.log('========没有跳转对话id了');
					self.lognode.opacity = 0;
				},1000);
			}

			//这里可能要加个检测判断条件的，暂时用true代替
			
			cc.log('clickTouch:this._logid='+this._logid);	
		}
	},
	//所有动作action函数
 	actfunc(animationid){
		let aniid = String(animationid);
/*1=魔王maou 2=勇者 3=法师 4=黑女仆 5=史莱姆1小的 6=史莱姆2大的史莱彦 7=僵尸zonbi 
8=修女 9=女骑士zonbi_yuusya 10=骑士kishi */
		let mowang = this.rolenode.getChildByName('role1').getComponent(cc.Sprite);
		let yongzhe = this.rolenode.getChildByName('role2').getComponent(cc.Sprite);
		let fashi = this.rolenode.getChildByName('role3').getComponent(cc.Sprite);
		let nvpu = this.rolenode.getChildByName('role4').getComponent(cc.Sprite);
		let shilaimu = this.rolenode.getChildByName('role5').getComponent(cc.Sprite);
		let shilaiyan = this.rolenode.getChildByName('role6').getComponent(cc.Sprite);
		let jiangshi = this.rolenode.getChildByName('role7').getComponent(cc.Sprite);
		let xiunv = this.rolenode.getChildByName('role8').getComponent(cc.Sprite);
		let nvqishi = this.rolenode.getChildByName('role9').getComponent(cc.Sprite);
		let qishi = this.rolenode.getChildByName('role10').getComponent(cc.Sprite);
		let mubei = this.rolenode.getChildByName('role11').getComponent(cc.Sprite);
		switch(aniid){//移入法师图片
			case '20':
				fashi.node.runAction(cc.moveTo(1,cc.p(-460, 0)));
				break;
			case '1':
				//移入勇者图片，魔王挨一刀
				let yzact = cc.moveBy(1,cc.p(620, 0));
				yongzhe.node.runAction(yzact);
				this.damagemovie(mowang.node);
				break;
			case '2'://勇者挨一刀，向左移动
				let self = this;
				setTimeout(function(){
					self.damagemovie(yongzhe.node);
				},1000);
				
				yongzhe.node.runAction(cc.moveBy(1,cc.p(-30, 0)));
				break;
			case '3'://勇者法师图片向左移出画面
				yongzhe.node.runAction(cc.moveTo(1,cc.p(-800, 0)));
				fashi.node.runAction(cc.moveTo(1,cc.p(-800, 0)));
				break;
			case '4'://黑女仆图片移入，右边栏显示黑女仆头像
				nvpu.node.runAction(cc.moveTo(1,cc.p(370, 0)));
				
				let nphead = this.itemnode.getChildByName('nvpuSprite').getComponent(cc.Sprite);
				nphead.node.runAction(cc.moveTo(1,cc.p(552, 277)));
				break;
			case '5'://黑女仆图片上下跳两下
				nvpu.node.runAction(cc.jumpTo(2, cc.p(370, 30), 30, 2));
				//global.gkjindu = 1;
				break;
			case '6'://【隐藏对话框】
				global._pasue = 1;
				this.lognode.opacity = 0;
				global._wait = 1;
				break;
			case '7'://【出现选择框】
				global._wait = 0;
				let count = this.panelnode.childrenCount;
				if(count > 0){
					this.panelnode.removeAllChildren();
				}
				let choicepanel = cc.instantiate(this.choicepanel);
				choicepanel.parent = this.panelnode;
				cc.director.emit('choicepanel',this._logid);
				global._pasue = 1;					
				break;
			case '8'://移入4个史莱姆
				let oldpos=[cc.p(-888,-144),cc.p(-888,8),cc.p(-1120,-144),cc.p(-1120,8)];
				//let newpos=[cc.p(-288,-144),cc.p(-288,8),cc.p(-520,-144),cc.p(-520,8)];
				for(let j=3;j>=0;j--){
					let slm = cc.instantiate(shilaimu.node);
					slm.parent = this.rolenode;
					//-404 -68:,-288,-144,,-288,8,,-520,8,,-520,-144
					slm.position = oldpos[j];
					slm.runAction(cc.moveBy(1,cc.p(600, 0)));
				}
				break;
			case '9'://【游戏结束】
				global._pasue = 1;
				global._wait = 0;
				this.mubunode.active = true;
				cc.log('~~~~~~this.mubunode.active = true;');
				let over = cc.instantiate(this.overclear);
				over.parent = this.panelnode;
				cc.director.emit('overclear',{type:'over',gkid:this._gkid});
				
				//触发结局存档
				this.saveend(this._logid);
				break;
			case '10'://【游戏存档】,废弃====
				this.saveend(this._logid);
				cc.log('=actfun 10-------------');
				break;
			case '11'://【缩放场景并恢复】
				//this.bgsprite.node.runAction(cc.scaleBy(1,1.2));
				let action = cc.sequence(cc.scaleTo(0.5,1.2),cc.scaleTo(0.5,1))
				this.node.runAction(action);
				break;
			case '12'://【获得香水】
				if(!this.isitem){//this.isitem为假意味着没有多余的xiangshui道具框
					this.lognode.opacity = 0;
					global._wait = 1;
					let xiangshui = cc.instantiate(this.itempanel);
					xiangshui.parent = this.itemnode;
					this.isitem = true;
					cc.director.emit('itempanel','xiangshui');
				}
				
				break;
			case '13'://【获得修女】
				if(!this.isitem){//this.isitem为假意味着没有多余的xiangshui道具框
					this.lognode.opacity = 0;
					global._wait = 1;
					let xiunvitem = cc.instantiate(this.itempanel);
					xiunvitem.parent = this.itemnode;
					this.isitem = true;
					cc.director.emit('itempanel','xiunv');
				}
				break;
			case '14'://【游戏通关】
				global._pasue = 1;
				global._wait = 0;
				this.mubunode.active = true;
				let clear = cc.instantiate(this.overclear);
				clear.parent = this.panelnode;
				cc.director.emit('overclear',{type:'clear',gkid:this._gkid});
				//触发结局存档
				this.saveend(this._logid);
				break;
			case '15'://僵尸图片被翻转为女骑士
				//治疗僵尸的帧动画播放1秒钟写死
				this.zhiliaomovie(jiangshi.node);
				setTimeout(function(){
					nvqishi.node.runAction(cc.scaleTo(1,1,1));
				},2000);
				setTimeout(function(){
					jiangshi.node.runAction(cc.scaleTo(1,0,1));
				},1000);
				
				
				break;
			case '16'://两个骑士图片出现
				let qs = cc.instantiate(qishi.node);
				qs.parent = this.rolenode;
				qs.position = cc.p(-900,0);
				qishi.node.runAction(cc.moveTo(1,cc.p(280,0)));
				qs.runAction(cc.moveTo(1,cc.p(-280,0)));
				break;
			case '17'://【转场黑屏】
				this.mubunode.active = true;
				this._gkid += 1;
				global.gkjindu = this._gkid;
				//跳转下一章节
				
				this.startstory(this._gkid);
				
				break;
			case '18'://更换bgm为Bad
				cc.director.emit('changebg',{name:'bad'});
				
				break;
			case '19'://更换bgm为clear
				cc.director.emit('changebg',{name:'clear'});
				
				break;
			default:
				break;
		}
		
		global._pasue = 0;
	}, 
	//显示菜单
	showmenu(){
		//检测panelnode下是否有预制件避免重复出现菜单界面
		let count = this.panelnode.childrenCount;
		if(count > 0){
			this.panelnode.removeAllChildren()
		}
		let menu = cc.instantiate(this.menu);
		menu.parent = this.panelnode;
		global._pasue = 1;
	},
	//场景初始化
	gameinit(gkid){
		this.bgsprite.spriteFrame = this.bgarr[gkid].spriteFrame;
		
		let mowang = this.rolenode.getChildByName('role1').getComponent(cc.Sprite);
		let yongzhe = this.rolenode.getChildByName('role2').getComponent(cc.Sprite);
		let fashi = this.rolenode.getChildByName('role3').getComponent(cc.Sprite);
		let nvpu = this.rolenode.getChildByName('role4').getComponent(cc.Sprite);
		let shilaimu = this.rolenode.getChildByName('role5').getComponent(cc.Sprite);
		let shilaiyan = this.rolenode.getChildByName('role6').getComponent(cc.Sprite);
		let jiangshi = this.rolenode.getChildByName('role7').getComponent(cc.Sprite);
		let xiunv = this.rolenode.getChildByName('role8').getComponent(cc.Sprite);
		let nvqishi = this.rolenode.getChildByName('role9').getComponent(cc.Sprite);
		let qishi = this.rolenode.getChildByName('role10').getComponent(cc.Sprite);
		let mubei = this.rolenode.getChildByName('role11').getComponent(cc.Sprite);
		//完全清除场景内role的痕迹
		this.clearscene(mowang,yongzhe,fashi,nvpu,shilaimu,shilaiyan,jiangshi,xiunv,nvqishi,qishi,mubei);
		switch(gkid){
			case 0://序章背景故事
				//预设本章节的role坐标
				mowang.node.x = 129;
				nvpu.node.x = 750;
				yongzhe.node.x = -800;
				fashi.node.x = -800;
				
				break;
			case 1://章节1
				//预设本章节的role坐标
				shilaimu.node.x = -404;
				shilaimu.node.y = -68;
				shilaiyan.node.x = 217;
				mowang.node.x = 900;
				nvpu.node.x = 900;
				//保持女仆的头像在道具栏
				let nphead = this.itemnode.getChildByName('nvpuSprite').getComponent(cc.Sprite);
				nphead.node.runAction(cc.moveTo(1,cc.p(552, 277)));
				
				shilaimu.node.addComponent(cc.Button);
				let shilaimuclick = new cc.Component.EventHandler();
				shilaimuclick.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
				shilaimuclick.component = "gamescene";//这个是代码文件名
				shilaimuclick.handler = "onRoleClick";
				shilaimuclick.customEventData = "shilaimu";
				let slmbutton = shilaimu.node.getComponent(cc.Button);
				slmbutton.clickEvents.push(shilaimuclick);
				
				shilaiyan.node.addComponent(cc.Button);
				let shilaiyanclick = new cc.Component.EventHandler();
				shilaiyanclick.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
				shilaiyanclick.component = "gamescene";//这个是代码文件名
				shilaiyanclick.handler = "onRoleClick";
				shilaiyanclick.customEventData = "shilaiyan";

				let slybutton = shilaiyan.node.getComponent(cc.Button);
				slybutton.clickEvents.push(shilaiyanclick);
				break;
			case 2://章节2
				global._wait = 1;
				//预设本章节的role坐标
				jiangshi.node.x = 176;
				jiangshi.node.scaleX = 1;
				nvqishi.node.x = 163;
				nvqishi.node.scaleX = 0;
				xiunv.node.x = -1780;
				mubei.node.x = -1144;
				nvpu.node.x = -1140;
				qishi.node.x = 900;
				this.bgsprite.node.x =0;
				this.bgsprite2.node.x = -1280;
				
				global._pasue = 1;
				this.lognode.opacity = 0;
				this.leftbtn.active = true;
				
				jiangshi.node.addComponent(cc.Button);
				let jiangshiclick = new cc.Component.EventHandler();
				jiangshiclick.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
				jiangshiclick.component = "gamescene";//这个是代码文件名
				jiangshiclick.handler = "onRoleClick";
				jiangshiclick.customEventData = "jiangshi";
				let jsbutton = jiangshi.node.getComponent(cc.Button);
				jsbutton.clickEvents.push(jiangshiclick);
				
				xiunv.node.addComponent(cc.Button);
				let xiunvclick = new cc.Component.EventHandler();
				xiunvclick.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
				xiunvclick.component = "gamescene";//这个是代码文件名
				xiunvclick.handler = "onRoleClick";
				xiunvclick.customEventData = "xiunv";
				let xiunvbutton = xiunv.node.getComponent(cc.Button);
				xiunvbutton.clickEvents.push(xiunvclick);
				
				nvpu.node.addComponent(cc.Button);
				let nvpuclick = new cc.Component.EventHandler();
				nvpuclick.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
				nvpuclick.component = "gamescene";//这个是代码文件名
				nvpuclick.handler = "onRoleClick";
				nvpuclick.customEventData = "nvpu2";
				let nvpubutton = nvpu.node.getComponent(cc.Button);
				nvpubutton.clickEvents.push(nvpuclick);
				
				mubei.node.addComponent(cc.Button);
				let mubeiclick = new cc.Component.EventHandler();
				mubeiclick.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
				mubeiclick.component = "gamescene";//这个是代码文件名
				mubeiclick.handler = "onRoleClick";
				mubeiclick.customEventData = "mubei";
				let mubeibutton = mubei.node.getComponent(cc.Button);
				mubeibutton.clickEvents.push(mubeiclick);
				break;
			default:
				break;
		}
	},
	
	//章节初始化开始后处理：开始本关、下一关和重新开始本关的调用
	startstory(gkid){
		this._gkid = gkid;
		//通过gameinit()来初始化角色图片的位置比较好
		this.gameinit(gkid);

		this._logid = global.titleinfo[gkid].startlogid;
		this.title.string = global.titleinfo[gkid].name;
		this.titlemovie();
	},
	
	//点击role角色才能出现对话的按钮点击操作
	onRoleClick(event,customData){
		cc.log('onRoleClick==global._wait='+global._wait);
		if(global._wait == 1){
			this.lognode.opacity = 255;
			global._pasue = 0;
			switch(customData){
				case 'shilaimu':
					this._logid = 42;
					this.showlogtxt(this._logid);
					break;
				case 'shilaiyan':
					this._logid = 63;
					this.showlogtxt(this._logid);
					break;
				case 'nvpu':
					this._logid = 40;
					this.showlogtxt(this._logid);
					global._pasue = 0;
					break;
				case 'jiangshi':
					this._logid = 69;
					this.showlogtxt(this._logid);
					break;
					
				case 'mubei':
					this._logid = 76;
					this.showlogtxt(this._logid);
					break;
					
				case 'xiunv':
					this._logid = 83;
					this.showlogtxt(this._logid);
					break;
				case 'nvpu2':
					this._logid = 79;
					this.showlogtxt(this._logid);
					break;
				default:
					break;
			}
		
		}
	},
	//存储结局信息
	saveend(logid){
		cc.log('saveend:logid------='+logid);
		for(let i = global.logidforend.length-1;i>=0;i--){
			if(logid == global.logidforend[i].logid){
				let endid= global.logidforend[i].endid;
				global.endlist[endid].reach = 1;
			}
		}
		cc.sys.localStorage.setItem('gkjindu',JSON.stringify(global.gkjindu));
		cc.sys.localStorage.setItem('endlist',JSON.stringify(global.endlist));
		cc.log('====saveend====');
		//此处的40是写死的，没有意义：
		//仅仅是为了实现：一开始背景故事切换到一章节内容时就存进度
		if(global.gkjindu < 1 && this._logid <= 40){
			global.gkjindu = 1;
		}else if(global.gkjindu <= this._gkid){
			global.gkjindu = this._gkid;
			cc.log('saveend:this._gkid='+this._gkid);
		}
	},
	//完全清理场景内role的痕迹，放置图片到屏幕外远处
	clearscene(mowang,yongzhe,fashi,nvpu,shilaimu,shilaiyan,jiangshi,xiunv,nvqishi,qishi,mubei){
		mowang.node.x = 5000;
		nvpu.node.x = 5000;
		yongzhe.node.x = 5000;
		fashi.node.x = 5000;
		shilaimu.node.x = 5000;
		shilaimu.node.y = 0;
		shilaiyan.node.x = 5000;
		jiangshi.node.x = 5000;
		nvqishi.node.x = 5000;
		xiunv.node.x = 5000;
		mubei.node.x = 5000;
		qishi.node.x = 5000;
	},
	rightarrow(){
		this.rightbtn.active = false;
		this.leftbtn.active = true;
		this.lognode.opacity = 0;
		
		let nvpu = this.rolenode.getChildByName('role4').getComponent(cc.Sprite);
		let jiangshi = this.rolenode.getChildByName('role7').getComponent(cc.Sprite);
		let xiunv = this.rolenode.getChildByName('role8').getComponent(cc.Sprite);
		let mubei = this.rolenode.getChildByName('role11').getComponent(cc.Sprite);
		cc.director.getScheduler().schedule(() => {
			this.bgsprite.node.x -= 20;
			this.bgsprite2.node.x -= 20;
			xiunv.node.x -= 20;
			mubei.node.x -= 20;
			nvpu.node.x -= 20;
			jiangshi.node.x -= 20;
		}, this, 1/64, 63);
	},
	leftarrow(){
		this.rightbtn.active = true;
		this.leftbtn.active = false;
		this.lognode.opacity = 0;
		
		let nvpu = this.rolenode.getChildByName('role4').getComponent(cc.Sprite);
		let jiangshi = this.rolenode.getChildByName('role7').getComponent(cc.Sprite);
		let xiunv = this.rolenode.getChildByName('role8').getComponent(cc.Sprite);
		let mubei = this.rolenode.getChildByName('role11').getComponent(cc.Sprite);
		cc.director.getScheduler().schedule(() => {
			this.bgsprite.node.x += 20;
			this.bgsprite2.node.x += 20;
			xiunv.node.x += 20;
			mubei.node.x += 20;
			nvpu.node.x += 20;
			jiangshi.node.x += 20;
		}, this, 1/64, 63);
	},

});
